using UnityEngine;
using System.Collections;

public class Game1 : MonoBehaviour {

	
	GUIStyle style = new GUIStyle();
	
	
	float textPosY = Screen.height/2 - 250;
	float textPosX = Screen.width/2 - 250;
	
	string keyToType;
	int keysToWin;
	int random;
	int clock = 0;
	
	bool gameOver = false;
		
	
	private ArrayList  letters;
	
	
	// Use this for initialization
	void Start () {
		letters = new ArrayList();
		letters.Add("A");
		letters.Add("E");
		letters.Add("I");
		letters.Add("O");
		letters.Add("U");
		
		newKeySet();
	}
	
	// Update is called once per frame
	void Update () {
		if(!gameOver){
			if (clock > 240){
				Debug.Log("GAME OVER");
				gameOver = true;
			}
			else{
				clock ++;
			}
			if(keyToType == "A"){
				if(Input.GetKeyDown(KeyCode.A)){
					clock = 0;	
					newKeySet();
				}
			}
			if(keyToType == "E"){
				if(Input.GetKeyDown(KeyCode.E)){
					clock = 0;	
					newKeySet();
				}
			}
			if(keyToType == "I"){
				if(Input.GetKeyDown(KeyCode.I)){
					clock = 0;	
					newKeySet();
				}
			}
			if(keyToType == "O"){
				if(Input.GetKeyDown(KeyCode.O)){
					clock = 0;	
					newKeySet();
				}
			}
			if(keyToType == "U"){
				if(Input.GetKeyDown(KeyCode.U)){
					clock = 0;	
					newKeySet();
				}
			}
		}
		else{
			if(Input.GetKeyDown(KeyCode.Escape)){
				Application.LoadLevel("Menu");	
			}
		}
	}
	
	// Function to set a new key to be pressed
	// and check if any more key can be used
	void newKeySet (){		
		keysToWin = letters.Count;
		if(keysToWin > 0){
			random = Random.Range(0, keysToWin);
			keyToType = (string) letters[random];			
			letters.RemoveAt(random);
		}else{
			Debug.Log("VENCEU");
			Application.LoadLevel("Winner");
		}
	}
	
	
	
	void OnGUI () {
		if(!gameOver){
			style.fontSize = 48;
	    	style.normal.textColor=Color.white;
	    	style.alignment=TextAnchor.MiddleCenter;
			GUI.Label (new Rect (textPosX, textPosY, 500, 300), 
			"Press key"+"  "+keyToType			
			,style);
		}else{
			style.fontSize = 72;
	    	style.normal.textColor=Color.white;
	    	style.alignment=TextAnchor.MiddleCenter;
			GUI.Label (new Rect (textPosX, 50, 500, 300), 
			"GAME OVER \n"+
			"You typed "+(5-keysToWin)+" correct \n \n "+
			"Press Esc to restart"
			,style);
		}
		
	}
}